﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class System_MenuGL : ATPages
{
    ZSql zs = new ZSql();
    ZSql zsp = new ZSql();
//    string pidstr = "";
    //InsertVisible="false"
    protected void Page_Load(object sender, EventArgs e)
    {
        lblMsg.Text = "";

        if (!IsPostBack)
        {
            treebind();
        }
    }

    public void treebind()
    {
        zs.Open("select * from Menu");

        TreeViewClass.InitTreeNoUrl(this.TreeView1, new DataTable[] { zs.m_table }, new string[] { "parentid" }, new string[] { "menuid" }, new string[] { "menu" }, null, null, null, null);

        zs.Close();
    }


    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        Hidden1.Value = TreeView1.SelectedValue;
        if (TreeView1.SelectedNode.Parent != null)
            Hidden2.Value = TreeView1.SelectedNode.Parent.Value;
        else
            Hidden2.Value = "0";

        zs.Open("select menu,url,pname=(select menu from menu a where a.menuid=menu.parentid) from menu where menuid = " + Hidden1.Value);
        zs.NextRow();

        txtname.Text = zs["menu"].ToString();
        txturl.Text = zs["url"].ToString();
        lblPname.Text = zs["pname"].ToString();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Hidden1.Value == "")
        {
            lblMsg.Text = "请选择一个父菜单";
            return;
        }
        else if (txtname.Text == "")
        {
            lblMsg.Text = "菜单名称不能为空";
            return;
        }
        else
        {
            if (txturl.Text != "")
                zs.Open("insert into menu(menu,url,parentid) values('" + txtname.Text + "','" + txturl.Text + "'," + Hidden1.Value + ")");
            else
                zs.Open("insert into menu(menu,parentid) values('" + txtname.Text + "'," + Hidden1.Value + ")");
            zs.Close();


            Response.Write("<script>alert('添加成功');window.location='MenuGL.aspx';</script>");
        }
        UpateMenu();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (txtname.Text == "")
        {
            lblMsg.Text = "菜单名称不能为空";
            return;
        }
        else
        {
            zs.Open("insert into menu(menu,url,parentid) values('" + txtname.Text + "','" + txturl.Text + "',0)");
            zs.Close();
            Response.Write("<script>alert('添加成功');window.location='MenuGL.aspx';</script>");
        }
        UpateMenu();
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        if (Hidden1.Value == "")
        {
            lblMsg.Text = "请选择要编辑的菜单";
            return;
        }
        else if (txtname.Text == "")
        {
            lblMsg.Text = "菜单名称不能为空";
            return;
        }
        //else if (txturl.Text == "")
        //{
        //    lblMsg.Text = "菜单地址不能为空";
        //    return;
        //}
        else
        {
            if (txturl.Text != "")
                zs.Open("update menu set menu='" + txtname.Text + "',url='" + txturl.Text + "' where menuid=" + Hidden1.Value);
            else
                zs.Open("update menu set menu='" + txtname.Text + "' where menuid=" + Hidden1.Value);
            zs.Close();
            Response.Write("<script>alert('修改成功');window.location='MenuGL.aspx';</script>");
        }
        UpateMenu();
    }


    protected void btnDel_Click(object sender, EventArgs e)
    {
        if (Hidden1.Value == "")
        {
            lblMsg.Text = "请选择要删除的菜单";
            return;
        }

        zsp.Open("select * from menu where parentid=" + Hidden1.Value);
        if (!zsp.NextRow())
        {
            zs.Open("delete menu where menuid=" + Hidden1.Value);
            zs.Close();
            Response.Write("<script>alert('删除成功');window.location='MenuGL.aspx';</script>");
        }
        else
        {
            lblMsg.Text = "不能删除有子菜单的菜单";
            return;
        }
    }

    private void UpateMenu()
    {

        zs.Open("update menu set node='父节点',menunode=0");
        zs.Open("update menu set node='子节点',menunode=1 where url!=''");
        zs.Close();

    }

}
